﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KornyezetVedelem;
using System.Runtime.InteropServices;
namespace Formok
{
    public partial class Szallitok : Form
    {
        const int MF_BYPOSITION = 0x400;
        [DllImport("User32")]
        private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
        [DllImport("User32")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [DllImport("User32")]
        private static extern int GetMenuItemCount(IntPtr hWnd);
        private KorVedelem kor;
        private int melyik = 0;
        
        private KornyezetVedelem.Szallitok_Prog ob;
        private String str = "Hiba";
        private Ideiglenes ideiglenes;
        private bool ok;
        private String ideiglenes_Nev="", atrako_Nev="";
        private int ora = -1;
        public Szallitok(KorVedelem kor)
        {
            IntPtr hMenu = GetSystemMenu(this.Handle, false);
            int menuItemCount = GetMenuItemCount(hMenu);                //a kilepes menu kiveve
            RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
            this.kor = kor;
            InitializeComponent();
        }
        public void Aktival()
        {
            Nev.Enabled = true;
            Megse.Enabled = true;
            Mentes.Enabled = true;
        }
        public void DezAktival()
        {
            Nev.Enabled = false;
            Mentes.Enabled = false;
            Megse.Enabled = false;
        }
        private void Szallitok_Load(object sender, EventArgs e)
        {
            bool er = false;
            ob = new Szallitok_Prog(ref er);
            DataSet dS = ob.SzallitokToltDS(ref str);
            dataGridView1.DataSource = dS.Tables["Tablak"];
            DezAktival();
            ideiglenes = new Ideiglenes(ref ok);
            ideiglenes.feltoltListBox(ideiglenes_l);
            ob.feltoltListBox(atrako_l);
            ideiglenes_l.Enabled = false;
            atrako_l.Enabled = false;
            ideiglenes_l.SelectedIndex = 0;
            atrako_l.SelectedIndex = 0;
        }

        private void Kilpes_button_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            kor.Show();
        }

        private void Beszuras_Click(object sender, EventArgs e)
        {
            Aktival();
            melyik = 1;
        }

        private void Torles_Click(object sender, EventArgs e)
        {
            DezAktival();
            if (dataGridView1.CurrentRow != null)
            {
                if (MessageBox.Show("Biztos vagy benne h torolni akarod ezt a  sort?\n", "Torles", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    String id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    Exception ex1 = ob.Torol(id);
                    if (ex1 != null)
                        MessageBox.Show("Nem Tudtam Torolni ezt a sort!\n\n" + Convert.ToString(ex1));
                    else
                    {

                        DataSet dS = ob.SzallitokToltDS(ref str);
                        dataGridView1.DataSource = dS.Tables["Tablak"];
                        MessageBox.Show("Sikeresen torolve...");
                    }
                }
            }
            else
                MessageBox.Show("Nincs torlendo sor az adatbazisban");
        }

        private void Modositas_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                Aktival();
                melyik = 2;
            }
            else
                MessageBox.Show("Nincs Modositando sor az adatbazisban!");
        }

        private void Megse_Click(object sender, EventArgs e)
        {
            DezAktival();
        }

        private void Mentes_Click(object sender, EventArgs e)
        {
            if (melyik == 1)
            {
                String nev;
                int nagysag;
                String tipus;
                if (Nev.Text == "")
                    MessageBox.Show("Kerem irjon be egy nevet A HulladeklerakoNev hez");
                else
                {
                    nev = Nev.Text;
                    ob.Beszur(nev);
                    DataSet dS = ob.SzallitokToltDS(ref str);
                    dataGridView1.DataSource = dS.Tables["Tablak"];
                    DezAktival();
                }
            }
            else
                if (melyik == 2)
                {
                    String nev;
                    String id;
                    id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    if (Nev.Text == "")
                        MessageBox.Show("Kerem irjon be egy nevet A HulladeklerakoNev hez");
                    else
                    {
                        nev = Nev.Text;
                        ob.Modosit(id, nev);
                        DataSet dS = ob.SzallitokToltDS(ref str);
                        dataGridView1.DataSource = dS.Tables["Tablak"];
                        DezAktival();
                    }

                }
           }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                ideiglenes_l.Enabled = true;
                atrako_l.Enabled = true;
                ideiglenes_Nev = ideiglenes_l.Text;
                atrako_Nev = atrako_l.Text;
                DataSet dS = ob.Szuro(ideiglenes_Nev, atrako_Nev,ora);
                dataGridView1.DataSource = dS.Tables["Tablak"];
            }
            else
            {
                ideiglenes_Nev = "";
                atrako_Nev = "";
                ideiglenes_l.Enabled = false;
                atrako_l.Enabled = false;
                DataSet dS = ob.SzallitokToltDS(ref str);
                dataGridView1.DataSource = dS.Tables["Tablak"];
            }
        }

        private void atrako_l_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (atrako_l.Enabled == false)
            {
                ideiglenes_Nev = "";
                atrako_Nev = "";
            }
            else
            {
                atrako_Nev = atrako_l.Text;
                DataSet dS = ob.Szuro(ideiglenes_Nev, atrako_Nev,ora);
                dataGridView1.DataSource = dS.Tables["Tablak"];
            }
        }

        private void ideiglenes_l_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ideiglenes_l.Enabled == false) {
                ideiglenes_Nev = "";
                atrako_Nev = "";
            }
            else
            {
                ideiglenes_Nev = ideiglenes_l.Text;
                DataSet dS = ob.Szuro(ideiglenes_Nev, atrako_Nev,ora);
                dataGridView1.DataSource = dS.Tables["Tablak"];
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
                try
                {
                    String s = textBox1.Text;
                    ora = Convert.ToInt32(s);
                    DataSet dS = ob.Szuro(ideiglenes_Nev, atrako_Nev, ora);
                    dataGridView1.DataSource = dS.Tables["Tablak"];
                }
                catch (Exception e1)
                {
                    ora = -1;
                    MessageBox.Show("Helytelen Karakter a Fizetes szuronel!\n");
                    DataSet dS = ob.Szuro(ideiglenes_Nev, atrako_Nev, ora);
                    dataGridView1.DataSource = dS.Tables["Tablak"];
                }
            else
            {
                ora = -1;
                DataSet dS = ob.Szuro(ideiglenes_Nev, atrako_Nev, ora);
                dataGridView1.DataSource = dS.Tables["Tablak"];
            }
        }
    }
}
